//
//  LGAddMedicineTimeCell.m
//  XieHeHeartDisease
//
//  Created by 1234 on 2024/3/4.
//

#import "LGAddMedicineTimeCell.h"

@interface LGAddMedicineTimeCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIImageView *selectView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UIImageView *tipView;
/**   */
@property (nonatomic , strong)UIButton *selectBtn;
/**   */
@property (nonatomic , copy)NSString *key;

@end

@implementation LGAddMedicineTimeCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}


-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    self.key = LGNSString(dataDic[@"key"]);
}

-(void)setResultDic:(NSDictionary *)resultDic{
    _resultDic = resultDic;
    if([resultDic isKindOfClass:[NSDictionary class]] && resultDic.allKeys.count>0){
        NSString *content = LGNSString(resultDic[self.key]);
        NSString *unit = LGNSString(resultDic[@"unit"]).length>0?LGNSString(resultDic[@"unit"]):@"";
        NSString *colorName = (content.length>0)?@"#666666":@"#999999";
        self.contentLabel.font = (content.length>0)?LGFont(16):LGFont(15);
        self.contentLabel.textColor = [UIColor colorWithString:colorName];
        self.contentLabel.text = (content.length>0)?[NSString stringWithFormat:@"%@%@",content,unit]:@"请选择";
        CGFloat width = [content textWidthWithFont:LGFont(16) maxHeight:MAXFLOAT];
        NSTextAlignment alignment = (width>Screen_W-viewPix(138))?NSTextAlignmentLeft:NSTextAlignmentRight;
        self.contentLabel.textAlignment = alignment;
        NSString *iconName = (content.length>0)?@"selected20":@"unselect20";
        self.selectView.image = [UIImage imageNamed:iconName];
    }
}

-(void)selectBtnAction{
    NSString *content = LGNSString(self.resultDic[self.key]);
    if(content.length>0 && self.clearSelectResult){
        self.clearSelectResult(self.key);
    }
}

-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
//    self.contentView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.selectView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.contentLabel];
    [self.baseView addSubview:self.tipView];
    [self.baseView addSubview:self.selectBtn];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.left.equalTo(self).offset(viewPix(16));
        make.right.equalTo(self).offset(-viewPix(16));
        make.height.equalTo(@(viewPix(40)));
    }];
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.width.height.equalTo(@(viewPix(18)));
        make.centerY.equalTo(self.baseView);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.selectView.mas_right).offset(viewPix(6));
        make.width.equalTo(@(viewPix(70)));
        make.centerY.equalTo(self.selectView);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(viewPix(10));
        make.right.equalTo(self.tipView.mas_left);
        make.centerY.equalTo(self.selectView);
    }];
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.width.height.equalTo(@(viewPix(16)));
        make.centerY.equalTo(self.selectView);
    }];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.baseView);
        make.width.equalTo(@(viewPix(50)));
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UIImageView *)selectView{
    if(!_selectView){
        _selectView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
    }
    return _selectView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"请选择" colorString:@"#999999" textFont:LGFont(15) textAlignment:NSTextAlignmentRight lines:2];
    }
    return _contentLabel;
}

-(UIImageView *)tipView{
    if(!_tipView){
        _tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    }
    return _tipView;
}

-(UIButton *)selectBtn{
    if(!_selectBtn){
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectBtn addTarget:self action:@selector(selectBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
